<template>
  <div class="index">
    <div master>
      <div class="item-h">
        <span class="ftz16">计划管理</span>
        <router-link to="/plan/new" class="fr">创建计划</router-link>
      </div>
      <div class="con-c">
        <div class="con-c-s">
          <form class="layui-form"  lay-filter="go">
            <div class="layui-col-md5">
              <label class="layui-form-label">起始时间：</label>
              <div class="layui-input-inline">
                <input type="text" class="layui-input date" name="beginTime"  placeholder="选择时间" id="startTime">
                <i>-</i>
                <input type="text" class="layui-input date" name="endTime" placeholder="选择时间" id="endTime">
              </div>
            </div>
            <div class="state layui-col-md4">
              <label class="layui-form-label">状态筛选：</label>
              <div class="layui-input-inline layui-col-md4">
                <select v-model="searchData.statusText" name="statusText" lay-verify="required">
                  <option v-for="item in statusTextArr" :value="item.val" :key="item.val">{{item.txt}}</option>
                </select>
              </div>
            </div>
          </form>
          <div class="state layui-input-inline">
            <button @click="search">查询</button>
          </div>
        </div>
        <table>
          <tr>
            <th>编号</th>
            <th>计划名称</th>
            <th>价格</th>
            <th>期限</th>
            <th>购买人数</th>
            <th>计划状态</th>
            <th>开始时间</th>
            <th>结束时间</th>
            <th>操作</th>
          </tr>
          <tr v-for="(item, index) in planList" :key="item.id">
            <td>{{(index + 1)}}</td>
            <td>{{item.name}}</td>
            <td>{{item.price}}</td>
            <td>{{item.timeLimit}}</td>
            <td>{{item.saleNum === null ? 0 : item.saleNum}}</td>
            <td>{{item.statusText}}</td>
            <td>{{item.beginTime | parseTime('{y}-{m}-{d}')}}</td>
            <td>{{item.endTime | parseTime('{y}-{m}-{d}')}}</td>
            <td><router-link :to="'/plan/edit/'+item.planId">修改</router-link></td>
          </tr>
        </table>
        <button v-if="total > this.planList.length" class="load-more ftz16" @click="loadMore">
          <template v-if="isLoadingMore">
            <i class='layui-icon layui-icon-loading layui-anim layui-anim-rotate layui-anim-loop ftz20'></i>
            <span>正在加载，请稍候......</span>
          </template>
          <span v-else>点击加载更多...</span>
        </button>
        <div v-else class="load-more ftz16">
          <span>数据已经全部加载</span>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import Laypage from '@/components/laypage'
import * as api from '@/service/api'
import { parseTimeToStamp } from '@/utils/assist'
let $ = window.$
export default {
  components: {
    Laypage
  },
  filters: {
    saleFlagFilter (val) {
      return ['停售', '运行中'][val]
    }
  },
  data () {
    return {
      viewData: {},
      total: null,
      siteId: '123530',
      layerLoad: null,
      searchData: {
        beginTime: '',
        endTime: '',
        pageSize: 10,
        pageNum: 1,
        statusText: ''
      },
      statusTextArr: [
        {
          val: '',
          txt: '全部'
        },
        {
          val: '预售',
          txt: '预售'
        },
        {
          val: '进行中',
          txt: '进行中'
        },
        {
          val: '进行结束',
          txt: '进行结束'
        },
        {
          val: '停售',
          txt: '停售'
        }
      ],
      planList: [],
      isLoadingMore: false
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.showLoadingModal()
      this.getPlanList()
      this.initDatePicker()
    })
  },
  methods: {
    showLoadingModal () {
      if (window.layer) {
        this.layerLoad = window.layer.load(0, {
          shade: false
        })
      }
    },
    closeLoadingModal () {
      if (window.layer) {
        window.layer.close(this.layerLoad)
      }
    },
    initDatePicker () {
      window.layui.use(['form', 'laydate'], () => {
        var form = window.layui.form
        form.on('select', (data) => {
          this.$set(this.searchData, $(data.elem).attr('name'), data.value)
        })

        // 日期插件
        let laydate = window.layui.laydate
        laydate.render({
          elem: '#startTime',
          done: (value, date, endDate) => {
            this.searchData.beginTime = parseTimeToStamp(value)
          }
        })
        laydate.render({
          elem: '#endTime',
          done: (value, date, endDate) => {
            this.searchData.endTime = parseTimeToStamp(value)
          }
        })
        form.render()
      })
    },
    // 查询
    search () {
      this.searchData.pageNum = 1
      this.getPlanList(false)
    },
    loadMore () {
      this.searchData.pageNum = this.searchData.pageNum + 1
      this.isLoadingMore = true
      this.getPlanList(true)
    },
    async getPlanList (isLoadMore) {
      let res = await api.getPlanListForAuthor({ ...this.searchData })
      if (!isLoadMore) {
        this.planList = []
        this.closeLoadingModal()
      }
      this.planList.push(...res.data.data.list)
      this.total = res.data.data.total
      this.isLoadingMore = false
    }
  }
}
</script>
<style lang="scss" scoped>
.con .item-h{
  padding: 0 3%;
}
.con .item-h span{
  color: #f23e3e;
  border-bottom: 3px solid #f23e3e;
  padding-bottom: 14px;
}
.con .item-h a{
  display: inline-block;
  height: 26px;
  width: 78px;
  text-align: center;
  line-height: 26px;
  border-radius: 13px;
  color: #fff;
  background: #00a0e9;
  margin: 13px 0;
}
.con .con-c{
  padding-bottom: 30px;
}
.con-c .con-c-s{
  padding: 30px 3%;
}
.con-c .con-c-s .date{
  width: 120px;
  display: inline-block;
  position: relative;
  padding: 0;
  text-indent: 20px;
  // background-image: url('../../../assets/res/img/icon.png');
  background-repeat: no-repeat;
  background-position: 78px -116px;
  cursor: pointer;
}
.con-c .con-c-s i{
  margin: 0 10px;
}
.con-c .con-c-s .state{
  display: inline-block;
  margin-left: 30px;
}
.con-c .con-c-s .state select{
  appearance:none;
  -moz-appearance:none;
  -webkit-appearance:none;
  width: 86px;
  height: 34px;
  border:1px solid #e6e6e6;
  text-indent: 10px;
  border-radius: 2px;
  color: #808080;
  background-repeat: no-repeat;
  background-position: 52px -118px;
  padding-left: 6px;
}
.con-c .con-c-s .state button{
  width: 70px;
  height: 32px;
  border-radius: 16px;
  border: 1px solid #00a0e9;
  color: #00a0e9;
  margin-left: 20px;
}
.con-c .con-c-s .state button:hover{
  color: #fff;
  background: #00a0e9;
}
.con-c table{
  width: 100%;
}
.con-c table th{
  font-weight: bold;
  font-size: 16px;
  height: 42px;
  background: #fafafa;
  border-right: 1px solid #e6e6e6;
  width: 40px;
}
.con-c table th:last-child{
  border-right: 0;
}
.con-c table tr td{
  height: 42px;
  text-align: center;
  border-right: 1px solid #e6e6e6;
  border-bottom: 1px solid #e6e6e6;
}
.con-c table tr td:last-child{
  border-right: 0;
}
.con-c table a{
  color: #4a7cc8;
}
.con-c .load_more{
  display: block;
  width: 94%;
  height: 40px;
  margin: 20px auto 0 auto;
  color: #808080;
  border: 1px solid #e6e6e6;
}
.load-more{
    display: block;
    margin: 0 auto;
    width: 94%;
    height: 38px;
    line-height: 38px;
    border:1px solid #e6e6e6;
    color: #808080;
    border-radius: 2px;
    background: #fff;
    margin-top: 20px;
    text-align: center;
}
</style>
